package cn.gov.pbc.gd.credit.provider.api.role.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.liu.jbase.service.db.impl.DBBaseServiceImpl;
import com.liu.tools.PrimaryUtil;

import cn.gov.pbc.gd.credit.api.role.IRoleApi;
import cn.gov.pbc.gd.credit.provider.dao.menu.IMenuDao;
import cn.gov.pbc.gd.credit.provider.dao.role.IRoleDao;
import cn.gov.pbc.gd.credit.provider.dao.rolemenu.IRoleMenuDao;
import cn.gov.pbc.gd.credit.vo.menu.ResMenuVo;
import cn.gov.pbc.gd.credit.vo.role.ResRoleVo;
import cn.gov.pbc.gd.credit.vo.role.RoleVo;
import cn.gov.pbc.gd.credit.vo.rolemenu.RoleMenuVo;

@Service
public class RoleApiImpl extends DBBaseServiceImpl<ResRoleVo, RoleVo, IRoleDao> implements IRoleApi{
    
	@Autowired
	private IRoleDao roleDao;
	@Autowired
	private IRoleMenuDao roleMenuDao;
	@Autowired
	private IMenuDao menuDao;
	
	
	@Override
	public int insert(RoleVo role) {
		
		int t = roleDao.insert(role);
		if(t == 1){
			
			List<String> menuList  = role.getMenuList();
			List<String> rootMenuList  = role.getRootMenuList();
			List<String> list = role.getRootMenuList();
			if(list == null){
				list = new ArrayList<>();
			}
			if(rootMenuList != null && rootMenuList.size() > 0){
				for (int i = 0; i < menuList.size(); i++) {
					String[] subIds = menuList.get(i).split("-");
					if(rootMenuList.contains(subIds[1])){
						continue ;
					}
					list.add(subIds[0]);
				}
			} else {
				list = menuList;
			}
			
			RoleMenuVo roleMenu = null;
			if(list != null && list.size() > 0){
				for (int i = 0; i < list.size(); i++) {
					roleMenu=new RoleMenuVo();
					roleMenu.setId(PrimaryUtil.primaryId("R"));
					roleMenu.setRoleId(role.getId());
					roleMenu.setMenuId(list.get(i));
					ResMenuVo resMenuVo = menuDao.queryById(list.get(i));
					roleMenu.setRootMenuId(resMenuVo.getParentId());
					roleMenu.setCreateBy(role.getCreateBy());
					roleMenu.setCreateTime(new Date());
					roleMenu.setUpdateBy(role.getUpdateBy());
					roleMenu.setUpdateTime(new Date());
					roleMenuDao.insert(roleMenu);
				}
			}
					
			return 1;
		}
		return 0;
	}
	
	
	@Override
	public int update(RoleVo roleVo) {
		
		int t=roleDao.update(roleVo);
		if(t==1){
			
			List<String> menuList  = roleVo.getMenuList();
			//删除原来所有的权限，此处为关联表，真删除
			RoleMenuVo rm=new RoleMenuVo();
			rm.setRoleId(roleVo.getId());
			roleMenuDao.delete(rm);
			
			List<String> rootMenuList  = roleVo.getRootMenuList();
			List<String> list = roleVo.getRootMenuList();
			if(list == null){
				list = new ArrayList<>();
			}
			if(menuList == null && rootMenuList == null){
				return 1;
			}
			
			if(menuList != null && menuList.size() > 0){
				for (int i = 0; i < menuList.size(); i++) {
					String[] subIds = menuList.get(i).split("-");
					if(rootMenuList != null && rootMenuList.size() > 0){
						if(rootMenuList.contains(subIds[1])){
							continue ;
						}
					}
					list.add(subIds[0]);
				}
			} 
			
			RoleMenuVo roleMenu = null;
			if(list != null && list.size() > 0){
				for (int i = 0; i < list.size(); i++) {
					roleMenu=new RoleMenuVo();
					roleMenu.setId(PrimaryUtil.primaryId("R"));
					roleMenu.setRoleId(roleVo.getId());
					roleMenu.setMenuId(list.get(i));
					ResMenuVo resMenuVo = menuDao.queryById(list.get(i));
					roleMenu.setRootMenuId(resMenuVo.getParentId());
					roleMenu.setCreateBy(roleVo.getCreateBy());
					roleMenu.setCreateTime(new Date());
					roleMenu.setUpdateBy(roleVo.getUpdateBy());
					roleMenu.setUpdateTime(new Date());
					roleMenuDao.insert(roleMenu);
				}
			}
			
			return 1;
		}
		return 0;
	}
	
	
	
}
